package com.soul.sys.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.soul.common.bean.vo.DictionaryVo;
import com.soul.common.controller.BaseController;
import com.soul.common.msg.ApiResult;
import com.soul.sys.bean.dto.dict.DictAddDto;
import com.soul.sys.bean.dto.dict.DictSearchDto;
import com.soul.sys.bean.dto.dict.DictUpdateDto;
import com.soul.sys.bean.vo.DictVo;
import com.soul.sys.service.DictService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Collection;
import java.util.Collections;
import java.util.List;

/**
 * <p>
 * 字典表 前端控制器
 * </p>
 *
 * @author sws
 * @since 2023-09-27 10:08:12
 */
@RestController
@RequestMapping("/dict")
@Api(value="dict", tags = "字典数据接口")
public class DictController extends BaseController {

    @Autowired
    private DictService dictService;

    /**
     * 查询字典数据,分页
     */
    @GetMapping("/{current}/{page}/list")
    @ApiOperation(value = "获取字典数据分页")
    public ApiResult<IPage<DictVo>> queryList(@PathVariable("current") Integer current,
                                                  @PathVariable("page") Integer page,
                                                  DictSearchDto searchDto){
        IPage<DictVo> dictVoIPage = this.dictService.queryDictList(current, page, searchDto);
        return ApiResult.success(dictVoIPage);
    }

    /**
     * 查询字典数据,分页
     */
    @GetMapping("/{typeCode}/list")
    @ApiOperation(value = "获取字典数据分页")
    public ApiResult<List<DictVo>> queryListByTypeCode(@PathVariable("typeCode") String typeCode){
        List<DictVo> dictVoIPage = this.dictService.queryDictByTypeCode(typeCode);
        return ApiResult.success(dictVoIPage);
    }

    @GetMapping("/{id}/info")
    @ApiOperation(value = "获取字典数据详细信息")
    public ApiResult<DictVo> getDict(@PathVariable Long id){
        DictVo dictInfo = this.dictService.getDictInfo(id);
        return ApiResult.success(dictInfo);
    }
    /**
     * 新增字典
     * @param addDto
     * @return
     */
    @PostMapping("/add")
    @ApiOperation(value = "新增字典数据")
    public ApiResult<?> add(@RequestBody DictAddDto addDto){
        this.dictService.addDict(addDto);
        return ApiResult.success();
    }

    /**
     * 修改字典
     * @param updateDto
     * @return
     */
    @PutMapping("/update")
    @ApiOperation(value = "修改字典数据")
    public ApiResult<?> add(@RequestBody DictUpdateDto updateDto){
        this.dictService.updateDict(updateDto);
        return ApiResult.success();
    }

    /**
     * 删除字典
     * @param id
     * @return
     */
    @DeleteMapping("/{id}/del")
    @ApiOperation(value = "删除字典数据，根据id")
    public ApiResult<?> delete(@PathVariable("id") Long id){
        this.dictService.deleteDict(id);
        return ApiResult.success();
    }



    /**
     * 查询字典
     * @return
     */
    @PostMapping("/query/dict/types")
    @ApiOperation(value = "根据类型查询数据字典")
    public ApiResult<List<DictionaryVo>> queryDictListByTypes(@RequestBody Collection<String> types){
        List<DictionaryVo> dictionaryVos = this.dictService.queryDictList(types);
        return ApiResult.success(dictionaryVos);
    }


    /**
     * 查询字典数据,分页
     */
    @GetMapping("/{typeCode}/select")
    @ApiOperation(value = "获取字典数据下拉框")
    public ApiResult<List<DictionaryVo>> querySelectByTypeCode(@PathVariable("typeCode") String typeCode){
        List<DictionaryVo> dictVoIPage = this.dictService.queryDictList(Collections.singletonList(typeCode));
        return ApiResult.success(dictVoIPage);
    }


}
